package demo;

import demo.mylinkedList.mylinkedlist;

public class HomeWork {
    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode(int val) {
            this.val = val;
        }
        public int getVal() {
            return val;
        }
    }

    public ListNode head;

    //合并两个有序链表
    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        //创建傀儡节点
        ListNode newHead = new ListNode(-1);//-1表示无效数据
        ListNode tmp = newHead;
        while (headA != null && headB != null) {
            if (headA.val < headB.val) {
                tmp.next = headA;
                headA = headA.next;
            } else {
                tmp.next = headB;
                headB = headB.next;
            }
            tmp = tmp.next;
        }
        if (headA != null) {
            tmp.next = headA;
        }
        if (headB != null) {
            tmp.next = headB;
        }
        return newHead.next;
    }

    //面试题 02.02.
    //返回倒数第 k 个节点
    public int kthToLast(ListNode head, int k) {
        if (k < 0) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int index = 0;
        while (index != k - 1) {
            fast = fast .next;
            if (fast == null) {
                return -1;
            }
            index++;
        }
        //同时走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    //获取链表的中间节点
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        //空链表
        if(head == null) {
            return null;
        }
        //只有头节点
        if(head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;

        while(cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }


    //移除链表元素
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;
                //cur = cur.next;
            } else {
                prev = cur;
                //cur = cur.next;
            }
            cur = cur.next;
        }
        if(head.val == val) {
            head = head.next;
        }
        return head;
    }

    //链表分割
    public ListNode partition(ListNode head, int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while (cur!= null) {
            if (cur.val < x) {
                //第一次插入
                if(bs == null) {
                    bs = be = cur;
                } else {  //不是第一次插入
                    be.next = cur;
                    be = cur;
                }
            } else {
                //第一次插入
                if(as == null) {
                    as = ae = cur;
                } else {  //不是第一次插入
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        //都大于x
        if (bs == null) {
            return as;
        }
        //前后接上
        be.next = as;
        //把结尾置空
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
        if (head == null) {
            return false;
        }
        if (head.next == null) {
            return true;
        }
        //1.找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转后半部分链表
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //3.一个从前往后走，一个从后往前走
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            //偶数情况
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //链表相交
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //1.假定A链表长，B链表短
        ListNode pl = headA;
        ListNode ps = headB;
        //2.分别求两个链表的长度
        int len1 = size(headA);
        int len2 = size(headB);
        //3.求长度差值len
        int len = len1 - len2;
        //4.确定pl指向的节点一定是长链表，ps指向的是短链表
        if (len < 0) {
            pl = headB;
            ps = headA;
            len = len2 - len1;
        }
        //5.让pl先走len步
        while (len != 0) {
            pl = pl.next;
            len--;
        }
        //6.ps和pl同时走，直到相遇！
        while (pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        return ps;
    }

    public int size(ListNode head) {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
}
